# !/usr/bin python3                                
# encoding: utf-8 -*-
# @file: datetime.py                       
# @author   : wjd
# @Time: 2024/5/9 16:28

import json
from datetime import datetime

# 原始的JSON数据
data = {
    "principalAmount": "249.99",
    "principalPending": "false",
    "interestRateRangeExResponses": [{
        "rateId": 14086630541122,
        "endTime": 1717171199000,
        "beginTime": 1714665599000,
        "loanAmount": "100",
        "interestRate": "0.22222",
        "operationType": "LOAN"
    }, {
        "rateId": 14086630541122,
        "endTime": 1717171199000,
        "beginTime": 1715011200000,
        "loanAmount": "50",
        "interestRate": "0.22222",
        "operationType": "OFFLINE_APPEND_LOAN"
    }, {
        "rateId": 14086630541122,
        "endTime": 1717171199000,
        "beginTime": 1715270400000,
        "loanAmount": "0.01",
        "interestRate": "0.22222",
        "operationType": "PAY_PRINCIPAL"
    }, {
        "rateId": 14086630541122,
        "endTime": 1717171199000,
        "beginTime": 1715270400000,
        "loanAmount": "100",
        "interestRate": "0.22222",
        "operationType": "OFFLINE_APPEND_LOAN"
    }]
}

# 将字符串转换为JSON对象
interest_rates = data["interestRateRangeExResponses"]
time_list = []
loanAmount_list = []
interestRate_list = []
# 遍历每个利率区间
for rate in interest_rates:
    # 提取 beginTime 和 endTime
    begin_time = rate["beginTime"]
    end_time = rate["endTime"]

    # 将时间戳转换为 datetime 对象
    begin_time_dt = datetime.fromtimestamp(begin_time / 1e3)  # 除以1e9是因为时间戳是以微秒为单位的
    end_time_dt = datetime.fromtimestamp(end_time / 1e3)

    # 打印转换后的时间

    print(f"开始时间: {begin_time_dt.strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"结束时间: {end_time_dt.strftime('%Y-%m-%d %H:%M:%S')}")
    print("\n")
    # 如果你想要打印所有的时间，可以去掉下面这行的注释
    # print("")  # 打印一个空行以便区分不同的利率区间
    # 计算两个时间点之间的时间差
    time_difference = end_time - begin_time

    # 将时间差转换为秒，然后除以 1000，加 1
    result = (time_difference / 1000) + 1
    time_list.append(result)
    loanAmount_list.append(rate["loanAmount"])
    interestRate_list.append(rate["interestRate"])
    # 打印结果

print("借款金额:{}".format(loanAmount_list))  # 借款金额
print("利率:{}".format(interestRate_list))  # 利率
print("\n")
print("借款金额:{}".format([format(float(i), '.5f') for i in loanAmount_list]))
print("利率:{}".format([float(i) for i in interestRate_list]))
print("利率时间:{}".format(time_list))
print("\n")
